#request 二开
import requests
from common import log_util
from loguru import logger
from config.project_baseinfo import baseurl
from common.assert_util import response_time
def send(name,#新加形参
        method,
        path,
        params=None,
        data=None,
        headers=None,
        cookies=None,
        files=None,
        auth=None,
        timeout=None,
        allow_redirects=True,
        proxies=None,
        hooks=None,
        stream=None,
        verify=None,
        cert=None,
        json=None,
        *args,
        **kwargs):
    try:
        #构造请求
        req_data = {
        'method': method,
        'url':baseurl+path,
        'params':params,
        'data':data,
        'headers':headers,
        'cookies':cookies,
        'files':files,
        'auth':auth,
        'timeout':timeout,
        'allow_redirects':allow_redirects,
        'proxies':proxies,
        'hooks':hooks,
        'stream':stream,
        'verify':verify,
        'cert':cert,
        'json':json
        }
        res = requests.session().request(**req_data)
        #开始编写日志相关内容
        logger.info(f'测试接口-----{name}-----')
        logger.info(f'测试地址：{res.request.url}')
        logger.info(f'请求体：{res.request.body}')
        logger.info(f'请求头：{res.request.headers}')

        #接口响应内容
        logger.info(f'响应信息：{res.status_code}----{res.reason}')
        logger.info(f'响应体:{res.json()}')
        logger.info(f'响应头:{res.headers}')
        logger.info(f'响应时间:{response_time(res)}毫秒')
        return res
    except Exception as e:
        logger.error(f'接口请求失败：{e}')